local UIBase = require("view.UIBase")
local MonsterBubble = class("MonsterBubble", UIBase)
local GameUtils = require("utils.GameUtils")
local Vector2 = require("utils.Vector2")

function MonsterBubble:ctor(parent, monsterId)
    self._monsterId = monsterId
    self._parent = parent
    local resource = {
        path = "Prefabs/View/Npc",
        name = "MonsterBubble",
        parent = parent
    }
    MonsterBubble.super.ctor(self, resource)
end

function MonsterBubble:start()
    local mainCamera = GameUtils.getMainCamera()
    self.cameraTransform = mainCamera.transform
    local forward = self.cameraTransform.forward
    self.transform.forward = forward
    self._canvas.worldCamera = mainCamera

    local cfg = require("config.monster_team").get(self._monsterId)
    self._textChat.text = cfg.name

    local bgRect = self._imageBg.gameObject:GetComponent(typeof(CS.UnityEngine.RectTransform))
    local size = bgRect.sizeDelta
    local prefredWidth = self._textChat.preferredWidth
    bgRect.sizeDelta = Vector2.new(prefredWidth + 20, size.y)
    self._imageBg.onClick:AddListener(handler(self, self._onMonsterTalkClick))
end

function MonsterBubble:_onMonsterTalkClick()
    printDebug(" MonsterBubble:_onMonsterTalkClick " .. self._monsterId)
    local monster_team = G_UserData:getGameCache():getFight_monster_team()
    local isHaveUI = G_UIManager:isHaveUIInScene()
    if self._monsterId > 0 and monster_team == 0 and not isHaveUI then
        G_UserData:getGameCache():setFight_monster_team(self._monsterId)
        local pos = self._parent.transform.position
        G_SignalManager:dispatch(SignalConst.EVENT_ENTER_FIGHT, pos)
    end
end

function MonsterBubble:update(dt)
    local forward = self.cameraTransform.forward
    self.transform.forward = forward
end

return MonsterBubble
